package com.wolfalpha.utron.service.impl;

import android.text.TextUtils;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemPartner;
import com.wolfalpha.utron.bean.PartnerInfo;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.PartnerService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodon on 15/8/20.
 */
public class PartnerServiceImpl implements PartnerService {


    @Override
    public List<ItemPartner> getList(int page, List<String> role, List<String> timein, List<Integer> level) {
        List<ItemPartner> partners = new ArrayList<ItemPartner>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        if (role != null && role.size() != 0) {
            for (int i = 0; i < role.size(); i++)
                mp.put("role[" + String.valueOf(i) + "]", role.get(i));
        }
        if (timein != null && timein.size() != 0) {
            for (int i = 0; i < timein.size(); i++)
                mp.put("timein[" + String.valueOf(i) + "]", timein.get(i));
        }
        if (level != null && level.size() != 0) {
            for (int i = 0; i < role.size(); i++)
                mp.put("level[" + String.valueOf(i) + "]", String.valueOf(level.get(i)));
        }
        String result = AppContext.getApiClient().sendPostResquest(URLs.PARTNER_URL + "getList", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemPartner project = JsonUtils.paraseJson(arr.get(i).toString(), ItemPartner.class);
                partners.add(project);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return partners;
    }

    @Override
    public PartnerInfo getSelfInfo() {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("password", AppContext.getInstance().getProperty("utron_pwd"));
        mp.put("own", "2");
        String result = AppContext.getApiClient().sendPostResquest(URLs.PARTNER_URL + "getInfo", mp);
        if (result == null || TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), PartnerInfo.class);
    }

    @Override
    public PartnerInfo getOtherInfo(int userId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(userId));
        mp.put("own", "1");
        String result = AppContext.getApiClient().sendPostResquest(URLs.PARTNER_URL + "getInfo", mp);
        if (result == null || TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        System.out.println(result);
        return JsonUtils.paraseJson(JsonUtils.getData(result), PartnerInfo.class);
    }

    @Override
    public boolean uptoGoodPartner(String role, String ability, String willing, String timein, String experience, String dy) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("role", role);
        mp.put("ability", ability);
        mp.put("willing", willing);
        mp.put("timein", timein);
        mp.put("experience", experience);
        mp.put("dy", dy);
        String result = AppContext.getApiClient().sendPostResquest(URLs.PARTNER_URL + "beGood", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean uptoAuthPartner(String yx, String nj, String aPic) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("nj", nj);
        mp.put("yx", yx);
        mp.put("auth_pic", aPic);
        String result = AppContext.getApiClient().sendPostResquest(URLs.PARTNER_URL + "beAuthed", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean updatePartner(String role, String ability, String willing, String timein, String experience, String dy) {
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("role", role);
        mp.put("ability", ability);
        mp.put("willing", willing);
        mp.put("timein", timein);
        mp.put("experience", experience);
        mp.put("dy", dy);
        String result = AppContext.getApiClient().sendPostResquest(URLs.PARTNER_URL + "update", mp);
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }
}
